#include <xpam/system/user_io.h>
namespace xpam
{
    namespace system
    {
        point<int> user_io::cursor_position()
        {
            POINT pt;
            ::GetCursorPos(&pt);
            return point<int>(pt.x, pt.y);
        }

        point<int> user_io::caret_position()
        {
            POINT pt;
            ::GetCaretPos(&pt);
            return point<int>(pt.x, pt.y);
        }

        bool user_io::key_state(virtual_key vk)
        {
            int flag = ::GetAsyncKeyState(vk);
            return (flag & 0x8000) == 0x8000;
        }

        void user_io::key_down(virtual_key vk)
        {
            INPUT inp;
            inp.type = INPUT_KEYBOARD;
            inp.ki.wVk = vk;
            inp.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
            inp.ki.time = 0;
            inp.ki.dwExtraInfo = 0;
            ::SendInput(1, &inp, sizeof(INPUT));
        }

        void user_io::key_up(virtual_key vk)
        {
            INPUT inp;
            inp.type = INPUT_KEYBOARD;
            inp.ki.wVk = vk;
            inp.ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP;
            inp.ki.time = 0;
            inp.ki.dwExtraInfo = 0;
            ::SendInput(1, &inp, sizeof(INPUT));
        }
    }
}